package com.honey.standard.writer;

import java.io.File;

import org.java.plugin.Plugin;
import org.java.plugin.PluginLifecycleException;

import com.honey.core.CorePlugin;
import com.honey.core.Extension;
import com.honey.core.compiler.CompilationDescriptor;
import com.honey.core.utils.ObjectFactory;
import com.honey.core.writer.CompilationWriter;
import com.honey.standard.writer.imp.JavaFileWriteImp;
import com.honey.standard.writer.imp.TextFileWriteImp;
import com.honey.standard.writer.imp.XmlFileWriteImp;

public class StandardWriterExtension extends Extension<Plugin> implements CompilationWriter {
	
	private JavaFileWrite javaWrite = null;
	
	private XmlFileWrite xmlWrite = null;
	
	private TextFileWrite textWrite = null;
	
	private StandardWriterPlugin standardWriterPlugin = null;
	
	private File targetFile = null;
	
	public StandardWriterExtension(Plugin belongPlugin) {
		super(belongPlugin);
		this.standardWriterPlugin = (StandardWriterPlugin)belongPlugin;
		
		try {	
			javaWrite = getJavaFileWrite() ;
			xmlWrite = getXmlFileWrite();
			textWrite = getTextFileWrite();
		} catch (PluginLifecycleException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void writeToFile(CompilationDescriptor compilationDescriptor) {
		switch(compilationDescriptor.getCompilationType() ){
			case JAVA:
				javaWrite.writeJavaFile(compilationDescriptor);
				targetFile = javaWrite.getTargetFile();
				break;
			case XML:
				xmlWrite.writeXmlFile(compilationDescriptor);
				targetFile = xmlWrite.getTargetFile();
				break;	
			default:
				textWrite.writeTextFile(compilationDescriptor);
				targetFile = textWrite.getTargetFile();
		}
		
	}
	
	private JavaFileWrite getJavaFileWrite() throws PluginLifecycleException{
		JavaFileWrite answer = null;
		com.honey.core.Extension<CorePlugin>  extension =  ObjectFactory.getExtensionClass(super.getBelongPlugin(), "JavaWriter");
		if( extension instanceof JavaFileWrite){
			answer = (JavaFileWrite)extension;
		}else{
			answer = new JavaFileWriteImp(standardWriterPlugin);
		}
		return answer ;
	}
	

	private XmlFileWrite getXmlFileWrite() throws PluginLifecycleException{
		XmlFileWrite answer = null;
		com.honey.core.Extension<CorePlugin>  extension =  ObjectFactory.getExtensionClass(super.getBelongPlugin(), "XmlWriter");
		if( extension instanceof XmlFileWrite){
			answer = (XmlFileWrite)extension;
		}else{
			answer = new XmlFileWriteImp(standardWriterPlugin);
		}
		return answer ;
	}
	
	private TextFileWrite getTextFileWrite() throws PluginLifecycleException{
		TextFileWrite answer = null;
		com.honey.core.Extension<CorePlugin>  extension =  ObjectFactory.getExtensionClass(super.getBelongPlugin(), "XmlWriter");
		if( extension instanceof XmlFileWrite){
			answer = (TextFileWrite)extension;
		}else{
			answer = new TextFileWriteImp(standardWriterPlugin);
		}
		return answer ;
	}
	
	@Override
	public void clean() {
		
	}

	@Override
	public File getTargetSourceFile() {
		
		return targetFile;
	}

}
